package com.tanhua.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.sso.pojo.Video;
import com.tanhua.util.IdWorker;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class VideoServiceApiImpi implements VideoServiceApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;
    //上传视频
    @Override
    public void save(Video video) {
        //设置vid
        Long videoId = idWorker.getNextId("video");
        video.setVid(videoId);
        mongoTemplate.save(video);
    }

    /*查看推荐视频*/
    @Override
    public List<Video> selectByVids(List<Long> vids) {
        Criteria vid = Criteria.where("vid").in(vids);
        List<Video> videoList = mongoTemplate.find(Query.query(vid).with(Sort.by(Sort.Order.desc("created"))), Video.class);
        return videoList;
    }


    //查看所有的视频
    @Override
    public List<Video> selectPage(int page, Integer pagesize) {
       Query query=new Query();
        List<Video> videoList = mongoTemplate.find(query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created"))), Video.class);
        return videoList;

    }

    //分页查看发布的视频,重载
    @Override
    public List<Video> selectPage(Integer page, Integer pagesize, Long uid) {
        Criteria criteria = Criteria.where("userId").is(uid);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return videoList;
    }
}
